/**
 * 
 */
package com.ffcs.crmd.tsp.server.netty.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.client.TspJobGroup;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.dto.page.PageInfo;
import com.ffcs.crmd.tsp.api.dto.page.RetVo;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.netty.ResponseCode;
import com.ffcs.crmd.tsp.core.util.BeanUtils;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.server.core.DataBaseHandler;
import com.ffcs.crmd.tsp.server.core.RemotingHandler;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobGroup;

import io.netty.channel.ChannelHandlerContext;

/**
 * 功能说明:
 *
 * @author ZHONGFUHUA
 * 
 * @since 
 *
 */
public class QueryJobGroupProcessor implements INettyProcessor {
    
    /**
     * 日志
     */
    private final static Logger LOG = LoggerFactory.getLogger(LoggerName.RemotingLoggerName);
    
    /**
     * 任务数据库服务
     */
    private DataBaseHandler     dataBaseHandler;
    
    /**
     * 功能说明:
     * 
     * @param remotingController
     */
    public QueryJobGroupProcessor(RemotingHandler remotingController, DataBaseHandler dataBaseHandler) {
        this.dataBaseHandler = dataBaseHandler;
    }
    
    /**
     * 功能说明:
     * 
     * @param ctx
     * @param request
     * @return
     */
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        RemotingCommand response = RemotingCommand.createResponseCommand(ResponseCode.SUCCESS);
        String requestXml = new String(request.getBody());
        try {
            LOG.debug("SERVER-NETTY: one request to query job group is received that body=[{}]", requestXml);
            TspJobGroup tspJobGroupVO = (TspJobGroup) XmlUtil.fromXml(requestXml, TspJobGroup.class);
            RetVo<TspJobGroup> tspJobGroupRetVo = new RetVo<TspJobGroup>();
            Integer pageNumber = tspJobGroupVO.getPageNumber();
            Integer pageSize = tspJobGroupVO.getPageSize();
            
            JobGroup jobGroupParam = new JobGroup();
            BeanUtils.applyIf(jobGroupParam, tspJobGroupVO);
            
            if (pageNumber != null && pageSize != null) {
                PageInfo<JobGroup> jobGroupPageInfo = dataBaseHandler.getServiceSupport().queryJobGroupPage(jobGroupParam);
                PageInfo<TspJobGroup> tspJobGroupPageInfo = new PageInfo<TspJobGroup>();
                List<TspJobGroup> tspJobGroupList = BeanUtils.copyList(jobGroupPageInfo.getList(), TspJobGroup.class);
                BeanUtils.applyIf(tspJobGroupPageInfo, jobGroupPageInfo);
                tspJobGroupPageInfo.setList(tspJobGroupList);
                tspJobGroupRetVo.setPageInfo(tspJobGroupPageInfo);
            } else if (tspJobGroupVO.getId() != null) {
                TspJobGroup tspJobGroup = new TspJobGroup();
                JobGroup jobGroup = dataBaseHandler.getServiceSupport().getJobGroup(jobGroupParam);
                BeanUtils.applyIf(tspJobGroup, jobGroup);
                tspJobGroupRetVo.setObject(tspJobGroup);
            } else {
                List<JobGroup> jobGroupList = dataBaseHandler.getServiceSupport().queryJobGroup(jobGroupParam);
                List<TspJobGroup> tspJobGroupList = BeanUtils.copyList(jobGroupList, TspJobGroup.class);
                tspJobGroupRetVo.setDataList(tspJobGroupList);
            }
            response.setBody(XmlUtil.toXml(tspJobGroupRetVo).getBytes());
        } catch (Exception e) {
            LOG.error("SERVER-NETTY: query job group[{}] exception:", requestXml, e);
            String responseBody = String.format("query job group:%s", e.getMessage());
            response.setCode(ResponseCode.FAIL);
            response.setBody(responseBody.getBytes());
        }
        return response;
    }
    
}
